package com.tt.fashionWebSite.service.imp;

import com.mysql.jdbc.ExceptionInterceptor;
import com.tt.fashionWebSite.dao.MauSacDao;
import com.tt.fashionWebSite.domain.CapBac;
import com.tt.fashionWebSite.domain.MauSac;
import com.tt.fashionWebSite.dto.CapBacDto;
import com.tt.fashionWebSite.dto.JsonResponseCode;
import com.tt.fashionWebSite.dto.MauSacDto;
import com.tt.fashionWebSite.service.Ad_ColorService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Service("ad_colorService")
public class Ad_ColorServiceImp extends GenericService implements Ad_ColorService {
    @Autowired
    @Qualifier("mauSac")
    private MauSacDao mauSacDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminColor(MauSacDto colorDto) {
        MauSac mauSac = mauSacDao.findById(colorDto.getMaMau());
        try {
            if(mauSac == null){
                mauSac = new MauSac();
                mauSac.setMaMau(colorDto.getMaMau());
                mauSac.setTenMau(colorDto.getTenMau());
                mauSacDao.insert(mauSac);
                return JsonResponseCode.SUCCESS;
            }
            else {
                return JsonResponseCode.FAILED;
            }
        }
        catch (Exception ex) {
            return JsonResponseCode.FAILED;
        }
    }

    public List<MauSacDto> getAllColor () {
        return mapper.mapCollection(mauSacDao.getAllColor(), MauSacDto.class);
    }

    public MauSacDto getColorInfoEdit(MauSacDto colorDto){
        MauSac mauSac = mauSacDao.findById(colorDto.getMaMau());
        MauSacDto mauSacDto = mapper.map(mauSac, MauSacDto.class,MAU_SAC_MAP_ID);
        return  mauSacDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public MauSacDto registerColorEdit(MauSacDto colorDto) {
        MauSac mauSac = mauSacDao.findById(colorDto.getMaMau());
        try {
            mauSac.setMaMau(colorDto.getMaMau());
            mauSac.setTenMau(colorDto.getTenMau());
            mauSacDao.update(mauSac);
            return colorDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteColor(List<MauSacDto> colorDto){
        for (int i = 0; i < colorDto.size(); i++) {
            try {
                mauSacDao.delete(colorDto.get(i).getMaMau());
            }
            catch (Exception ex) {
                return JsonResponseCode.FAILED;
            }
        }
        return JsonResponseCode.SUCCESS;
    }
}
